/*
 * Copyright (C) 2009 Rob Cash
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Main.java
 */

package org.robcash.apps.messagepopup;

import static org.robcash.apps.messagepopup.BeanNameConstants.SUBSCRIPTION_STORE_BEAN_NAME;
import static org.robcash.apps.messagepopup.BeanNameConstants.SUBSCRIPTION_SERVICE_BEAN_NAME;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.robcash.apps.messagepopup.view.MainView;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.robcash.apps.messagepopup.dataaccess.business.SubscriptionBD;
import org.robcash.apps.messagepopup.model.Model;
import org.robcash.apps.messagepopup.vo.SubscriptionStore;
import org.robcash.commons.task.ThreadPoolExecutor;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * The main class of the application.
 */
public class Main extends SingleFrameApplication {

    private ThreadPoolExecutor executor;
    private GenericBeanFactoryAccessor appContext;
    private SubscriptionStore subscriptionStore;

    public ThreadPoolExecutor getExecutor() {
        return executor;
    }

    /**
     * Get the Spring application context
     * @return Spring application context
     */
    public GenericBeanFactoryAccessor getSpringContext() {
        return appContext;
    }

    /**
     * Get the subscription store
     * @return Subscription store
     */
    public SubscriptionStore getSubscriptionStore() {
        return subscriptionStore;
    }

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        executor = new ThreadPoolExecutor(2, 10, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(10));
        appContext = new GenericBeanFactoryAccessor(
                new ClassPathXmlApplicationContext("/ApplicationContext.xml"));
        try {
            SubscriptionBD subscriptionService = appContext.getBean(
                    SUBSCRIPTION_SERVICE_BEAN_NAME);
            this.subscriptionStore = subscriptionService.loadSubscriptionStore();
            Model.getInstance().setSubscriptionStore(subscriptionStore);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
                    "Failed to load user's subscription store", ex);
            this.subscriptionStore = appContext.getBean(SUBSCRIPTION_STORE_BEAN_NAME);
        }
        show(new MainView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     * @param root Root window
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of Main
     */
    public static Main getApplication() {
        return Application.getInstance(Main.class);
    }

    /**
     * Main method launching the application.
     * @param args Program arguments
     */
    public static void main(String[] args) {
        launch(Main.class, args);
    }
}
